package com.outfoodmanage.utils;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author huisonglin
 * @Description
 * @PackageName intelligent-vehicle-platform
 * @Package com.intelligent.utils
 * @Date 2024-01-08 15:49
 * @Version 1.0
 */
public class IKUtils {

    public static void main(String[] args) {
        String str="我家住在黄土高坡";
        Map<String, String> map=segMore(str);
        System.out.println(map);
    }

    public static Map<String, String> segMore(String text) {
        Map<String, String> map = new HashMap<>();
        map.put("智能分词", segText(text, true));
//        map.put("最大分词", segText(text, false));
        return map;
    }

    private static String segText(String text, boolean useSmart) {
        StringBuilder result = new StringBuilder();
        IKSegmenter ik = new IKSegmenter(new StringReader(text), useSmart);
        try {
            Lexeme word = null;
            while((word=ik.next())!=null) {
                result.append(word.getLexemeText()).append(" ");
            }
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        return result.toString();
    }
}
